//
//  BMPickerViewToolBar.m
//  bigmama
//
//  Created by huyuchen on 2017/8/9.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMPickerViewToolBar.h"

@interface BMPickerViewToolBar ()
//背景
@property (nonatomic, strong) UIImageView *topBarView;
//左边按钮
@property (nonatomic, strong) UIButton *barBtnLeft;
//右边按钮
@property (nonatomic, strong) UIButton *barBtnRight;
//中间标题
@property (nonatomic, strong) UILabel *barLblTitle;

@property (nonatomic, copy) BtnClickBlock doneAction;
@property (nonatomic, copy) BtnClickBlock cancelAction;
@end

@implementation BMPickerViewToolBar

- (instancetype)initWithToolbarText:(NSString *)toolBarText style:(BMPickerViewToolBarStyle)style cancelBlock:(BtnClickBlock)cancelBlock doneBlock:(BtnClickBlock)doneBlock{
    if (self = [super init]) {
        _doneAction = [doneBlock copy];
        _cancelAction = [cancelBlock copy];
        _barLblTitle.text = toolBarText;
        [self changeThemeStyle:style];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI{
    UIImageView *topBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    [topBarView setBackgroundColor:[UIColor colorWithRed:71.0/255 green:206.0/255 blue:96.0/255 alpha:1.0]];
    [topBarView setUserInteractionEnabled:YES];
    topBarView.layer.borderColor = [UIColor colorWithRed:71.0/255 green:206.0/255 blue:96.0/255 alpha:1.0].CGColor;
    topBarView.layer.borderWidth = 0.5f;
    self.topBarView = topBarView;
    [self addSubview:topBarView];
    
    self.barBtnLeft = [[UIButton alloc] initWithFrame:CGRectMake(12, 5, 48, 34)];
    [_barBtnLeft setBackgroundImage:[UIImage imageNamed:@"btn_cancel_normal.png"] forState:UIControlStateNormal];
    [_barBtnLeft.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
    [_barBtnLeft addTarget:self action:@selector(barBtnLeftClick:) forControlEvents:UIControlEventTouchUpInside];
    [topBarView addSubview:_barBtnLeft];
    
    self.barBtnRight = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -60, 5, 48, 34)];
    [_barBtnRight setBackgroundImage:[UIImage imageNamed:@"btn_confirm_normal.png"] forState:UIControlStateNormal];
    [_barBtnRight.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
    [_barBtnRight addTarget:self action:@selector(barBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
    [topBarView addSubview:_barBtnRight];
    
    self.barLblTitle = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, SCREEN_WIDTH -120, 34)];
    [_barLblTitle setTextAlignment:NSTextAlignmentCenter];
    [_barLblTitle setBackgroundColor:[UIColor clearColor]];
    [_barLblTitle setTextColor:[UIColor whiteColor]];
    [topBarView addSubview:_barLblTitle];
}

- (void)changeThemeStyle:(BMPickerViewToolBarStyle)style{
    switch (style) {
        case BMPickerViewToolBarStyleIcon:
            [self.barBtnLeft setBackgroundImage:[UIImage imageNamed:@"btn_cancel_normal.png"] forState:UIControlStateNormal];
            [self.barBtnLeft setTitle:@"" forState:UIControlStateNormal];
            [self.barBtnRight setBackgroundImage:[UIImage imageNamed:@"btn_confirm_normal.png"] forState:UIControlStateNormal];
            [self.barBtnRight setTitle:@"" forState:UIControlStateNormal];
            [self.topBarView setBackgroundColor:[UIColor colorWithRed:71.0/255 green:206.0/255 blue:96.0/255 alpha:1.0]];
            self.topBarView.layer.borderColor = [UIColor colorWithRed:71.0/255 green:206.0/255 blue:96.0/255 alpha:1.0].CGColor;
            [self.barLblTitle setTextColor:[UIColor whiteColor]];
            
            break;
        case BMPickerViewToolBarStyleText:
            [self.barBtnLeft setBackgroundImage:nil forState:UIControlStateNormal];
            [self.barBtnLeft setTitle:@"取消" forState:UIControlStateNormal];
            [self.barBtnLeft setTitleColor:RGB(167, 160, 157) forState:UIControlStateNormal];
            [self.barBtnRight setBackgroundImage:nil forState:UIControlStateNormal];
            [self.barBtnRight setTitle:@"确定" forState:UIControlStateNormal];
            [self.barBtnRight setTitleColor:ColorNewGreen forState:UIControlStateNormal];
            [self.topBarView setBackgroundColor:kWhiteColor];
            self.topBarView.layer.borderColor = RGB(207,207,207).CGColor;
            [self.barLblTitle setTextColor:ColorDefalutBrown];
            break;
        default:
            break;
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.topBarView.frame = self.bounds;
    
    self.barBtnLeft.frame = CGRectMake(12, 5, 48, 34);
    
    self.barBtnRight.frame = CGRectMake(SCREEN_WIDTH -60, 5, 48, 34);
    
    self.barLblTitle.frame = CGRectMake(60, 5, SCREEN_WIDTH -120, 34);
    
}

- (void)barBtnRightClick:(UIButton *)btn{
    if (self.doneAction) {
        self.doneAction(btn);
    }
}

- (void)barBtnLeftClick:(UIButton *)btn{
    if (self.cancelAction) {
        self.cancelAction(btn);
    }
}

- (void)dealloc{
    NSLog(@"BMPickerViewToolBar dealloc");
}
@end
